//
//  ViewController.m
//  SPMapKit
//
//  Created by zhangbo on 2017/10/11.
//  Copyright © 2017年 zhangbo. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    _mapView = [[SPMapView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    _mapView.delegate = self;
    _mapView.mapProvider = GOOGLEMAP_PROVIDER;
    _mapView.mapType = SPMapTypeStandard;
    
    [self.view addSubview:_mapView];
    
    UISegmentedControl *menuView = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"地图",@"卫星",@"混合",@"空白", nil]];
    menuView.frame = CGRectMake(0, 0, 300, 35);
    menuView.center = CGPointMake(self.view.frame.size.width / 2, self.view.frame.size.height - 40);
    [menuView addTarget:self action:@selector(subMenu_clicked:) forControlEvents:UIControlEventValueChanged];
    [menuView setSelectedSegmentIndex:0];
    [self.view addSubview:menuView];
    
    CLLocationCoordinate2D a = CLLocationCoordinate2DMake(41.708424915957046, 123.43085206942439);
    CLLocationCoordinate2D b = CLLocationCoordinate2DMake(41.700437946359551, 123.43079264564714);
    CLLocationCoordinate2D c = CLLocationCoordinate2DMake(41.700611086660693, 123.43729679617755);
    CLLocationCoordinate2D d = CLLocationCoordinate2DMake(41.707755970825502, 123.43573312273824);
    
    // 多边形
    SPPolygon *gon = [[SPPolygon alloc] init];
    
    [gon addGPSPoint:a];
    [gon addGPSPoint:b];
    [gon addGPSPoint:c];
    [gon addGPSPoint:d];
    
    // 点
    [self addPoint:a];
    [self addPoint:b];
    [self addPoint:c];
    [self addPoint:d];
    
    [_mapView addOverlay:gon];
    
    // 圆
    SPCircle *circle = [[SPCircle alloc] initWithLocation:CLLocationCoordinate2DMake((a.latitude + c.latitude) / 2 , (a.longitude + c.longitude) / 2)];
    circle.radius = 20;
    
    [_mapView addOverlay:circle];
    
    MKCoordinateSpan span;
    span.latitudeDelta = 0.01;
    span.longitudeDelta = 0.01;
    
    MKCoordinateRegion region;
    region.center = circle.coordinate;
    region.span = span;
    
    [_mapView locateToRegion:region animated:YES];
}

-(void) addPoint:(CLLocationCoordinate2D) location
{
    SPDefaultAnnotation *anno = [[SPDefaultAnnotation alloc] init];
    anno.coordinate = location;
    [_mapView addAnnotation:anno];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void) subMenu_clicked:(UISegmentedControl*) segControl
{
    switch (segControl.selectedSegmentIndex) {
        case 0:
            _mapView.mapType = SPMapTypeStandard;
            break;
        case 1:
            _mapView.mapType = SPMapTypeSatellite;
            break;
        case 2:
            _mapView.mapType = SPMapTypeHybrid;
            break;
        case 3:
            _mapView.mapType = SPMapTypeBlank;
            break;
            
        default:
            _mapView.mapType = SPMapTypeStandard;
            break;
    }
}

#pragma mark - SPMapViewDelegate
-(SPAnnotationView*) mapView:(SPMapView*) mapView viewForAnnotation:(id <SPAnnotation>)annotation
{
    if ([annotation isKindOfClass:[SPDefaultAnnotation class]]) {
        SPImageAnnotationView *view = [[SPImageAnnotationView alloc] initWithAnnotation:annotation];
        view.imageView.image = [UIImage imageNamed:@"point"];
        [view sizeToFit];
        
        return view;
    }
    
    return nil;
}

-(SPOverlayRenderer *)mapView:(SPMapView *)mapView rendererForOverlay:(id <SPOverlay>)overlay
{
    if ([overlay isMemberOfClass:[SPPolyline class]]) {
        SPPolyLineRender *render = [[SPPolyLineRender alloc] initWithOverlay:overlay];
        render.strokeColor = [UIColor blueColor];
        return render;
    }
    
    if ([overlay isMemberOfClass:[SPPolygon class]]) {
        SPPolygonRender *render = [[SPPolygonRender alloc] initWithOverlay:overlay];
        render.strokeColor = [UIColor blueColor];
        render.fillColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.7];
        return render;
    }
    
    if ([overlay isMemberOfClass:[SPCircle class]]) {
        SPCircleRender *render = [[SPCircleRender alloc] initWithOverlay:overlay];
        render.strokeColor = [UIColor redColor];
        render.fillColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.7];
        return render;
    }
    
    return nil;
}

-(void) mapView:(SPMapView *)mapView didSelectBackground:(CLLocationCoordinate2D) gps
{
    [self addPoint:gps];
}

@end
